import requests
import traceback
import json
import os
import sys
import time

from xjlibrary.our_file_dir import BaseDir

curPath = os.path.dirname(os.path.abspath(__file__))
sGUrl = r'http://118.190.88.230:10100/DataHandler.ashx'
HEADERS = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive',
           'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)', }
sSqlList = list()
nCount = 0
StartTime = time.time()
dictParams = {
    'CheckType': 'GetListByLastUpdateTime',
    'BegTime': '2019-11-20',
    'EndTime': '2019-12-20',
    'Pageindex': '1',
    'Pagesize': '100'
}


# 测试文字输出到文本


def output(value, files='log.txt'):
    f = open(files, 'w', encoding='utf-8')
    f.write(value)
    f.close()


def GetPageNum():
    r = requests.get(sGUrl, headers=HEADERS, params=dictParams, timeout=20)
    text = r.content.decode('utf-8').strip()
    print(text)
    dic = json.loads(text)
    pageNum = dic['totalpage']
    if pageNum:
        print('pageNum:', pageNum)
        return pageNum
    else:
        return -1


# 下载详情页面
def DownPage():
    global sSqlList, nCount, dictParams
    wanID = None
    pageNum = GetPageNum()
    if -1 == pageNum:
        print('获取总页数失败')
        sys.exit(-1)
    for i in range(1, pageNum + 1):
        dictParams['Pageindex'] = str(i)
        r = None
        (bFlag, r) = RequestDetailPage()

        # 判断是否请求详情页成功 bFlag（1：成功，0：失败）
        sSql = ''
        if not bFlag:
            print('下载失败，index:%d' % i)
            sys.exit(-1)
        else:
            html = r.content.decode('utf-8').strip()
            line = html.replace('\0', ' ').replace(
                '\r', ' ').replace('\n', ' ') + '\n'

            # IP = socket.gethostbyname(socket.gethostname())
            # PID = os.getpid()
            dirPath = os.path.join(curPath, 'json')
            BaseDir.create_dir(dirPath)
            outPathFile = os.path.join(
                dirPath, dictParams['EndTime'] + '.big_htm')
            with open(outPathFile, mode='a', encoding='utf-8') as f:
                f.write(line)
                nCount += 1
                print('写入文件数量：%d' % nCount)
                print('耗费时间: %d' % int(time.time() - StartTime) + '秒')

            sSql = "UPDATE article set stat = 1 where wanID = {wanID}".format(
                wanID=wanID)
            print('DOWN:' + str(nCount))


# 请求详情页
def RequestDetailPage():
    sExMsg = None
    r = None
    try:
        r = requests.get(sGUrl, headers=HEADERS, params=dictParams, timeout=20)
    except:
        sExMsg = '* exMsg:' + traceback.format_exc()
    finally:
        if 'r' in locals() and r:
            r.close()
    if sExMsg:
        print(sExMsg)
        return False, r

    if r.status_code != requests.codes.ok:
        print('error-url:' + r.url)
        print('r.status_code' + str(r.status_code))
        return False, r

    print(r.url)

    return True, r


# ----------------------------------------------逻辑部分-----------------------
# ----------------------------------------------下载详情页----------------------

if __name__ == '__main__':
    # 开始作业

    DownPage()

    # print('DOWN TOTAL:' + str(nCount))
